package com.nulldev.util.JVM.reflections.cache;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import com.nulldev.util.data.Arrays.maps.trie.TrieMap;

final class FieldObject {
	private final Map<String, Field> map = new TrieMap<String, Field>();

	public FieldObject(Class<?> clazz) {
		Field[] fs = clazz.getDeclaredFields();
		if (fs != null) {
			int len = fs.length;
			for (int i = 0; i < len; i++) {
				Field f = fs[i];
				if (Modifier.isPublic(f.getModifiers())) {
					f.setAccessible(true);
					map.put(f.getName(), f);
				}
			}
		}
	}

	public boolean isField(boolean staticFlg, String name) {
		Field f = map.get(name);
		if (f != null) {
			return (Modifier.isStatic(f.getModifiers()) == staticFlg);
		}
		return false;
	}

	public void getNames(Set<String> out, boolean staticFlg) {
		String name;
		Iterator<String> it = map.keySet().iterator();
		while (it.hasNext()) {
			name = it.next();
			if (Modifier.isStatic(map.get(name).getModifiers()) == staticFlg) {
				out.add(name);
			}
		}
	}

	public Field get(boolean staticFlag, String name) {
		Field f = map.get(name);
		if (f != null && Modifier.isStatic(f.getModifiers()) == staticFlag) {
			return f;
		}
		return null;
	}
}
